package com.recruit.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.recruit.common.util.PageUtil;
import com.recruit.model.Message;
import com.recruit.service.IMessageService;
import com.recruit.vo.CreatedVO;
import com.recruit.vo.PageResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jikun
 * @since 2024-04-17
 */
@RestController
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private IMessageService messageService;
    @PostMapping("/add")
    public CreatedVO add(@RequestBody Message message)
    {
        message.setCreateTime(LocalDateTime.now());
        messageService.save(message);
        return new CreatedVO(200);
    }

    @GetMapping("/list")
    public PageResponseVO<Message> getMessageList(@RequestParam(name = "count", required = false, defaultValue = "15")
                                                      @Min(value = 1, message = "{page.count.min}")
                                                      @Max(value = 30, message = "{page.count.max}") Integer count,
                                                  @RequestParam(name = "page", required = false, defaultValue = "0")
                                                      @Min(value = 0, message = "{page.number.min}") Integer page){
        Page<Message> messagePage = new Page<>(page, count);
        LambdaQueryWrapper<Message> lq = new LambdaQueryWrapper<>();
        lq.orderByDesc(Message::getCreateTime);
        Page<Message> page1 = messageService.page(messagePage,lq);
        return PageUtil.build(page1);
    }

}
